/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.sistemasInf.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pe.edu.pucp.sistemasInf.bean.Usuario;

/**
 *
 * @author Cesar
 */
public class MySQLDAOUsuario implements DAOUsuario{

    @Override
    public int validateUser(String user, String pass) {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs=null;
        try {
            //Paso 1: Registramos el driver
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //Paso 2: Obtenemos la conexión            
            conn = new DBConfig().getConnection();
            //Paso 3: Preparamos la sentencia
            pstmt = conn.prepareStatement(
                    "SELECT * FROM usuario WHERE cod_usuario=? AND contraseña=?");
            
            pstmt.setString(1, user);
            pstmt.setString(2, pass);
            rs=pstmt.executeQuery();
            
            while (rs.next())
            {
                return rs.getInt("idPerfil");
            }
            
            
        } catch (SQLException ex) {
            Logger.getLogger(MySQLDAOUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {//Paso 5: Cerramos la conexión
            conn.close(); } catch(Exception g) {g.printStackTrace();}
        }
        
        return -1;
    }
    
}
